package com.u17;

import java.util.HashMap;

/**
 * @author 65
 * https://leetcode-cn.com/problems/roman-to-integer/
 */
public class Solution {

    public int romanToInt(String s) {

        HashMap<Character, Integer> hashMap = new HashMap<>();

        hashMap.put('I', 1);
        hashMap.put('V', 5);
        hashMap.put('X', 10);
        hashMap.put('L', 50);
        hashMap.put('C', 100);
        hashMap.put('D', 500);
        hashMap.put('M', 1000);


        Integer result = 0;
        for (int i = 0; i < s.length(); i++) {
            Integer resultChar = hashMap.get(s.charAt(i));
            if (i < (s.length() - 1) && resultChar < hashMap.get(s.charAt(i + 1))) {
                result = result - resultChar;
            } else {
                result = result + resultChar;
            }
        }

        return result.intValue();
    }


    /**
     * 使用Java中字符串替换的方式
     * @param s
     * @return
     */
    public int romanToInt2(String s) {
        s = s.replace("IV", "A");
        s = s.replace("IX", "B");
        s = s.replace("XL", "c");
        s = s.replace("XC", "d");
        s = s.replace("CD", "E");
        s = s.replace("CM", "F");

        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            result = result + getValue(s.charAt(i));
        }
        return result;
    }

    public static int getValue(char c) {
        switch (c) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            case 'A':
                return 4;
            case 'B':
                return 9;
            case 'c':
                return 40;
            case 'd':
                return 90;
            case 'E':
                return 400;
            case 'F':
                return 900;
            default:
                return 0;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int romanToInt = solution.romanToInt2("MCMXCIV");
        System.out.println(romanToInt);
    }
}
